<!doctype html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>Login Tutorial</title>
    <link href="style.css" type="text/css" rel="stylesheet"/>
</head>
<body>

<?php
if (isset($_POST["submit"])) {
    $errors = array();
    $required = array("email", "pword");
    foreach ($required as $item) {
        if (isset($_POST[$item]) and empty($_POST[$item])) {
            $errors[] = "The <strong> {$item} </strong> was left blank";
        }
    }

    if (empty($errors)) {
        $conn = mysqli_connect("localhost", 'root', '123', 'users') or die('Could not connect to DB');
        $email = mysqli_real_escape_string($conn, $_POST['email']);
        $pword = mysqli_real_escape_string($conn, $_POST['pword']);
        $hash_pw = sha1($pword);

        $query = "SELECT CONCAT_WS(' ',first_name,last_name) AS name FROM users2 WHERE email='$email' AND pword='{$pword}' LIMIT 1";
        $result = mysqli_query($conn, $query) or die(mysqlli_error($conn));
        if (mysqli_num_rows($result) == 1) {
            while ($rows = mysqli_fetch_array($result, MYSQL_ASSOC)) {
                session_start();
                $_SESSION['name'] = $rows['name'];
                header('Location: admin_panel.php');
            }
        } else {
            $errors[] = "The email or password do not match those on file.";
        }
    }

}
?>

<div id="wrapper">
    <?php
    if (!empty($errors)) {
        echo('<ul>');
        foreach ($errors as $error) {
            echo("<li>$error</li>");
        }
        echo('</ul>');
    }
    ?>
    <form action="tutorial.php" method="post">
        <p>
            <label for="email">Email</label>
            <input type="text" name="email"/>
        </p>

        <p>
            <label for="pword">Password</label>
            <input type="password" name="pword"/>
        </p>

        <p>
            <input type="submit" name="submit" value="Go"/>
        </p>

    </form>
    <a href="#" Login></a>
</div>
</body>
</html>